﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using CommonHelp;
using Core.Unity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Services; 
using ViewModels;

namespace Commands {
    /// <summary>
    /// 灾害预警
    /// </summary>
    public class QueryDisasterCommand : CommandBase {
        private IXContainer _hook;
        private IDisplayTable _IDisplayTable;

        public override void OnCreate(object hook) {
            base.OnCreate(hook);
            _hook = hook as IXContainer;

            _IDisplayTable = _hook.Resolve<IDisplayTable>();
        }

        public override void OnClick(object parameter) {
            try {
                base.OnClick(parameter);
                HttpHelper http = new HttpHelper("http://www.weather.com.cn/data/alarm_xml/alarminfo.xml");
                http.GetAsync(AfterGetData);
                this.IsEnabled = false;
            }
            catch (Exception ex) {
                CommonHelp.LogHelper.Error(ex);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="responseString"></param>
        public void AfterGetData(string responseString) {
            try {
                if (responseString == null) {
                    System.Windows.MessageBox.Show("获取数据失败，请稍后重试。");
                    return;
                }
                //XmlDocument doc = new XmlDocument();
                //doc.LoadXml(responseString);

                //List<DisasterInfo> infos = new List<DisasterInfo>();
                //for (int i = 0; i < doc.ChildNodes.Count; i++) {
                //    var node = doc.ChildNodes[i];
                //    if (node.NodeType == XmlNodeType.XmlDeclaration || node.NodeType == XmlNodeType.Comment) {
                //        continue;
                //    }
                //    for (int j = 0; j < node.ChildNodes.Count; j++) {
                //        var groupNode = node.ChildNodes[j];
                //        foreach (XmlNode item in groupNode.ChildNodes) {
                //            string jsonStr = JsonConvert.SerializeXmlNode(item, Newtonsoft.Json.Formatting.None, true);
                //            jsonStr = jsonStr.Replace("\"@", "\"");
                //            DisasterInfo info = JsonConvert.DeserializeObject<DisasterInfo>(jsonStr);
                //            info.Type = groupNode.Name;
                //            infos.Add(info);
                //        }
                //    }
                //}
                //_IDisplayTable.DisplayData = infos.ToArray();
                //_IDisplayTable.AddRowVisitor = new AddDisasterInfo(_hook.Resolve<DbHelp.DbHelper>());
                //_hook.Resolve<ITextHint>().AddText(DateTime.Now.ToString() + " 获取到 " + infos.Count.ToString() + " 条 灾害预警 数据。");
            }
            catch (Exception ex) {
                CommonHelp.LogHelper.Error(ex);
                System.Windows.MessageBox.Show(ex.Message, "出错了，请稍后重试。");
            }
            finally {
                this.IsEnabled = true;
                //_hook.Resolve<WebViewModel>().Url = "http://www.weather.com.cn/alarm/";
            }
        }
    }
}
